/*
 * 创建日期 2005-9-30
 *
 * 更改所生成文件模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
package com.vanda.report.element;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;

import com.vanda.report.ReportException;
import com.vanda.report.ReportInnerData;

/**
 * @author wkf5985
 *
 * 更改所生成类型注释的模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
public abstract class Element implements Cloneable, Serializable {
	
	//子元素集合
	private List subElements;
	
	private static final String PACKAGENAME = "com.vanda.report";
	
	public static Element getInstance(String subPackage,String type) throws Exception{
		if(subPackage == null){
			subPackage = "";
		}else{
			subPackage = "."+subPackage;
		}
		type = type.toUpperCase().substring(0,1)+type.substring(1);
		String name = PACKAGENAME+subPackage+"."+type+"Element";
		
		Class cls = null;
		
		try{
			cls = Class.forName(name);
		}catch(Exception e){
			//System.out.println(e);
		}
		//如果不在subPackage下，那么直接在com.vanda.report包下找
		try{
			if(cls == null){
				cls = Class.forName(PACKAGENAME+".element."+type+"Element");
			}
		}catch(Exception e){
			//System.out.println(e);
		}
		//如果还没找到,则从logic包下找
		try{
			if(cls == null){
				cls = Class.forName(PACKAGENAME+".logic."+type+"Element");
			}
		}catch(Exception e){
			//System.out.println(e);
		}
		return (Element)cls.newInstance();
	}
	
	/**
	 * 取得子元素需要的数据类
	 * @param parent
	 * @param child
	 * @param parentObject
	 * @return
	 */
	public Object getChildElementObject(Element parent,Element child,Object parentObject){
		return parentObject;
	}
	
	/**
	 * 标签执行方法，递归执行
	 * @param obj
	 */
	public void execute(Object obj) throws ReportException{
		//如果没有子元素，则什么都不做，直接返回
		if(subElements == null || subElements.size() == 0){
			return;
		}
		ReportInnerData reportData = (ReportInnerData)obj;
		Object tempData = reportData.getContextData();
		int start_row = reportData.getStart_row();
		int start_col = reportData.getStart_col();
		int formatRowNum = reportData.getFormatRowNum();
		for(int i=0;i<subElements.size();i++){
			//取得子元素
			Element child = (Element)subElements.get(i);
			//取得子元素需要的数据
			Object childObject = getChildElementObject(this,child,obj);
			//执行
			child.execute(childObject);
			
			//重新设置,因为可能会改变
			reportData.setContextData(tempData);
			reportData.setStart_row(start_row);
			reportData.setStart_col(start_col);
			reportData.setFormatRowNum(formatRowNum);
		}
	}
	
	public abstract void validator() throws ReportException;
	
	protected void setProperty(Object obj,String property,Object param) throws Exception{
		PropertyUtils.setProperty(obj,property,param);
	}
	
	protected Object getProperty(Object obj,String property) throws Exception{
		if(obj == null){
			return null;
		}
		return PropertyUtils.getProperty(obj,property);
	}
	
	/**
	 * @return
	 */
	public List getSubElements() {
		return subElements;
	}

	/**
	 * @param list
	 */
	public void setSubElements(List list) {
		subElements = list;
	}

	public Object clone() {
		try {
			return super.clone();
		} catch (CloneNotSupportedException cnse) {
			// This never happens
			return null;
		}
	}
}
